package com.example.whereismystuff.views;

import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.LoginDataBaseAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
/**
 * This view is in charge of all the process assigned with the admin view including vieing all users, adding users, deleting users , etc
 * 
 * @author NullPointers
 *
 */
public class AdminEdit extends Activity implements OnClickListener{
	String username;
	String password;
	String locked;
	String admin;
	Intent i;
	EditText i_username, i_password;
	CheckBox isLocked, isAdmin;
	Button save, cancel, delete;
	LoginDataBaseAdapter loginDataBaseAdapter;
	/**
	 * On create method that is called when Admin Edit is instatiated. Contacts teh LoginDataBAseAdapter and 
	 * makes a list of all of te users on the screen
	 */

	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_edit_layout);

		loginDataBaseAdapter=new LoginDataBaseAdapter(this);
		loginDataBaseAdapter=loginDataBaseAdapter.open();

		i_username = (EditText) findViewById(R.id.u_usernameInput);
		i_password = (EditText) findViewById(R.id.u_passwordInput);

		save = (Button) findViewById(R.id.e_save);
		delete = (Button) findViewById(R.id.e_delete);
		save.setOnClickListener(this);
		cancel = (Button) findViewById(R.id.e_Cancel);
		cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try{
					Class c = Class.forName("com.example.whereismystuff.views.UserView");
					i = new Intent(getApplicationContext(), c);
					startActivity(i);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}

		});
		isLocked = (CheckBox) findViewById(R.id.isLockedBox);
		isAdmin = (CheckBox) findViewById(R.id.isAdminBox);

		Bundle extras = getIntent().getExtras();
		if(extras != null){
			if(extras.getString("Username") != null){
				username = extras.getString("Username");
			}
			if(extras.getString("Password") != null){
				password = extras.getString("Password");
			}
			if(extras.getString("Locked") != null){
				locked = extras.getString("Locked");
			}
			if(extras.getString("Admin") != null) {
				admin = extras.getString("Admin");
			}
		}
		i_username.setText(username);
		i_password.setText(password);

		if(locked.equals("true"))
			isLocked.setChecked(true);
		else
			isLocked.setChecked(false);

		if(admin.equals("true"))
			isAdmin.setChecked(true);
		else
			isAdmin.setChecked(false);

	}
	public void onBackPressed() {
		return;
	}

	/**
	 * The onCLick method send the user to user view when a user is clicked on the list
	 * 
	 */
	@Override
	public void onClick(View v) {
		boolean isAdminBool = false;
		boolean isLockedBool = false;
		if(isLocked.isChecked())
			isLockedBool = true;
		if(isAdmin.isChecked())
			isAdminBool = true;
		loginDataBaseAdapter.updateEntry(username, password, String.valueOf(isLockedBool), String.valueOf(isAdminBool));	
		try{
			Class c = Class.forName("com.example.whereismystuff.views.UserView");
			i = new Intent(getApplicationContext(), c);
			startActivity(i);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * The on Delete method connects with the login database adapter and deletes teh user fomr it.
	 * It then returns to UserView
	 * @param v
	 */
	public void onDelete(View v){
		loginDataBaseAdapter.deleteEntry(username);
		Toast.makeText(getApplicationContext(), "User Deleted", Toast.LENGTH_SHORT).show();
		try{
			Class c = Class.forName("com.example.whereismystuff.views.UserView");
			i = new Intent(getApplicationContext(), c);
			startActivity(i);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
